import axios from 'axios'
import {
  history
} from 'umi'
import {
  message
} from 'antd'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {
  serverBaseUrl
} from '@/assets/js/ConstVariable'
export default function request(config) {
  // 创建axios实例
  const instance = axios.create({
    baseURL: `${serverBaseUrl}/admin/`,
    timeout: 5000,
  })

  // axios拦截器
  instance.interceptors.request.use(
    (config) => {
      // 展示进度条
      NProgress.start()
      return config
    },
    (err) => {},
  )

  instance.interceptors.response.use(
    (res) => {
      NProgress.done()
      // 隐藏进度条

      // 处理无权限
      if (res.data.status !== 200) {
        message.error(res.data.statusText)
        setTimeout(() => {
          history.replace('/login')
        }, 1000)
      }

      return res.data
    },
    (err) => {},
  )

  return instance(config)
}
